#pragma once
#include <iostream>
#include <assert.h>
#include <string.h>
#include <algorithm>

namespace yss
{
	template <class T>
	
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;


		size_t size() const
		{
			return _finish - _start;
		}
		size_t capacity() const
		{
			return _endofstage - _start;
		}
		void Swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_endofstage, v._endofstage);
		}

		vector()
		{}
		~vector()
		{
			delete[] _start;
			_start = _finish = _endofstage = nullptr;
		}
		iterator begin()
		{
			return _start;
		}
		iterator end()
		{
			return _finish;
		}
		const_iterator begin() const
		{
			return _start;
		}
		const_iterator end() const
		{
			return _finish;
		}

		void reserve(size_t n)
		{
			if (n > capacity())
			{
				T* tmp = new T[n];
				size_t old = _finish - _start;
				if (_start)
				{
					for (size_t i = 0; i < old; i++)
					{
						tmp[i] = _start[i];
					}
					delete[] _start;
				}
				_start = tmp;
				_finish = _start + old;
				_endofstage = _start + n;
			}
		}
		void resize(size_t n, const T& val)
		{
			if (n <= size())
			{
				_finish = _start + n;
			}
			else
			{
				if (n > capacity())
				{
					reserve(n);
				}
				size_t len = n - size();
				while (len--)
				{
					*_finish = val;
					_finish++;
				}
			}
		}

		void push_back(const T& val)
		{
			if (_finish == _endofstage)
			{
				size_t newcapacity = capacity() == 0 ? 4 : 2 * capacity();
				reserve(newcapacity);
			}
			*_finish = val;
			_finish++;
		}
		
		template <class InputIterator>
		vector(InputIterator first, InputIterator last)
		{
			reserve(last - first);
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}

		vector(size_t n, const T& val = T())
		{
			resize(n, val);
		}
		vector(int n, const T& val = T())
		{
			resize(n, val);
		}

		void pop_back()
		{
			assert(size() > 0);
			--_finish;
		}

		/*vector(const vector<T>& v)
		{
			_start = new T[v.capacity()];
			memcpy(_start, v._start, sizeof(T)*v.size());
			_finish = _start + v.size();
			_endofstage = _start + v.capacity();
		}*/

		vector(const vector<T>& v)
		{
			reserve(v.capacity());
			for (auto e : v)
			{
				push_back(e);
			}
		}

		/*vector<T>& operator=(const vector<T>& v)
		{
			if (this != &v)
			{
				T* tmp = new T[v.capacity()];
				memcpy(tmp, v._start, sizeof(T) * v.size());
				delete[] _start;
				_start = tmp;
				_finish = _start + v.size();
				_endofstage = _start + v.capacity();
			}
			return *this;
		}*/

		vector<T>& operator=(vector<T> v)
		{
			Swap(v);
			return *this;
		}

		iterator insert(iterator pos, const T& x)
		{
			assert(pos >= _start && pos <= _finish);
			size_t len = pos - _start;
			size_t old = size();
			if (_finish = _endofstage)
			{
				size_t newcapacity = capacity() == 0 ? 4 : 2 * capacity();
				reserve(newcapacity);
				pos = _start + len;
				_finish = _start + old;
			}
			iterator end = _finish - 1;
			while (end >= pos)
			{
				*(end + 1) = *end;
				end--;
			}
			*pos = x;
			_finish++;
			return pos;
		}

		iterator erase(iterator pos)
		{
			assert(pos >= _start && pos <= _finish);
			iterator begin = pos;
			while (begin < _finish - 1)
			{
				*begin = *(begin + 1);
				begin++;
			}
			_finish--;
			return pos;
		}

		T& operator[](size_t pos)
		{
			assert(pos < size());
			return _start[pos];
		}

	private:
		
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _endofstage = nullptr;
	};
}


